import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import jxl.Workbook;
import jxl.write.*;
import jxl.write.biff.RowsExceededException;
import jxl.read.biff.BiffException;

public class XlMaker {

	/**
	 * @param args
	 * @throws IndexOutOfBoundsException 
	 * @throws WriteException 
	 * @throws RowsExceededException 
	 * @throws IOException 
	 * @throws BiffException 
	 */
	public static void main(String[] args) throws RowsExceededException, WriteException, IndexOutOfBoundsException, IOException, BiffException {
		
			Workbook inWorkbook = Workbook.getWorkbook(new File("Templete.xls"));			
			WritableWorkbook outWorkbook = Workbook.createWorkbook(new File("output.xls"),inWorkbook);
			
			
			//각 파일명에 따라 탭 이름 변경.
			File curDirectory = new File(".");
			String Files[] = curDirectory.list();
			
			
			int sheetIndex = 0;	//sheetIndex
			for(String XMLFile:Files){
				//확장자가 XML이 아닌 것 통과.
				if (!XMLFile.substring(XMLFile.length()-3, XMLFile.length()).equalsIgnoreCase("xml")) continue;
				String fileNm = XMLFile.substring(0,XMLFile.length()-4);				
				
				//시트이름변경
				outWorkbook.getSheet(sheetIndex).setName(XMLFile.substring(0,XMLFile.length()-4));
				Label nameUI = new Label(1,0,XMLFile.substring(0,XMLFile.length()-4));

				outWorkbook.getSheet(sheetIndex).addCell(nameUI);
				
				//시트에 입력할 데이터 읽기.
				File srcFile = new File(fileNm+".txt");				
				System.out.println("Reading... "+fileNm + ".txt");				
								
				FileReader srcReader = new FileReader(srcFile);					
				BufferedReader buffer = new BufferedReader(srcReader);
				
				String strReadLine = new String();
				List<HashMap<String,String>> funcList = new ArrayList<HashMap<String,String>>();
				while((strReadLine = buffer.readLine()) != null){
					
					String tokens[] = strReadLine.split("\t");
					if (tokens.length != 2) continue;
					HashMap<String, String> tokenMap = new HashMap<String,String>();
					tokenMap.put("compNm", tokens[0]);
					tokenMap.put("event", tokens[1]);
					funcList.add(tokenMap);
				}
				
				for (int i = 0 ; i < funcList.size() ; i++){
					Label compNm = new Label(1,3+i,funcList.get(i).get("compNm"));
					Label event = new Label(2,3+i,funcList.get(i).get("event"));
					
					try {
						outWorkbook.getSheet(sheetIndex).addCell(compNm);
						outWorkbook.getSheet(sheetIndex).addCell(event);
					} catch (RowsExceededException e) {
						e.printStackTrace();
					} catch (WriteException e) {
						e.printStackTrace();
					} catch (IndexOutOfBoundsException e) {
						e.printStackTrace();
					}
				}
				sheetIndex++;
			}
			int sheetCnt = outWorkbook.getSheetNames().length;
			if (sheetIndex != 0){
				for (int i = sheetIndex ; i < sheetCnt ; i++)
					outWorkbook.removeSheet(i);
			}
			outWorkbook.write();
			
			inWorkbook.close();			
			outWorkbook.close();
	}

}
